<template>
  <div class="c-bg-white c-ph24 c-pt180" v-if="testRes!=null">
    <div class="c-textAlign-c c-fs38 c-fc-xblack c-text-ellipsis2">{{testRes.name}}</div>
    <div class="c-fs22 c-fc-gray c-textAlign-c c-mt32" v-if="examStatus==1">{{testRes.examTime}}测试结果</div>
    <div class="c-fs22 c-fc-gray c-textAlign-c c-mt32" v-if="examStatus==1 && ((testRes.scoringMethod != 1 && testRes.scoringMethod != 2 && testRes.scoringMethod != 3) || testRes.isCheck == -1)">—— ——</div>
    <div class="c-fs22 c-fc-gray c-textAlign-c c-mt32" v-if="examStatus==0 || examStatus==2"><span v-if="examStatus==0 || examStatus==2">共{{testRes.allNum}}题</span><span v-if="examStatus==2"> | 已测{{testRes.hasTestNum}}题</span></div>
    <div class="c-fs22 c-fc-gray c-textAlign-c c-mt24" v-if="examStatus==1 && testRes.isCheck != -1 && (testRes.scoringMethod==1 || testRes.scoringMethod==2 || testRes.scoringMethod==3)">{{testRes.scoringMethod==1?'正确率':testRes.scoringMethod==2?'总得分':'共答对'}}</div>
    <div class="c-fs48 theme-fc c-textAlign-c c-mt8" v-if="examStatus==1 && testRes.isCheck != -1 && testRes.scoringMethod==2">{{testRes.totalpoins}}<span class="c-fs24">分</span></div>
    <div class="c-fs48 theme-fc c-textAlign-c c-mt8" v-if="examStatus==1 && testRes.isCheck != -1 && testRes.scoringMethod==1">{{Number(testRes.accuracy*100).toFixed(0)+'%'}}</div>
    <div class="c-fs48 theme-fc c-textAlign-c c-mt8" v-if="examStatus==1 && testRes.isCheck != -1 && testRes.scoringMethod==3">{{testRes.rightNum}}<span class="c-fs24">题</span></div>
    <div class="c-fs28 c-fc-white c-ww440 c-hh72 c-lh72 c-br8 theme-bg c-textAlign-c c-center c-mt90" v-if="examStatus==0" @click="testNow">立即测试</div>
    <div class="c-fs28 c-fc-white c-ww440 c-hh72 c-lh72 c-br8 theme-bg c-textAlign-c c-center c-mt90" v-if="examStatus==2" @click="goOnTest">继续测试</div>
    <div class="c-fs28 c-fc-white c-ww440 c-hh72 c-lh72 c-br8 theme-bg c-textAlign-c c-center c-mt90" v-if="examStatus==1 && testRes.isCheck != -1" @click="analyze">查看解析</div>
    <div class="c-fs28 c-fc-white c-ww440 c-hh72 c-lh72 c-br8 c-bg-mgray c-textAlign-c c-center c-mt90" v-if="examStatus==1 && testRes.isCheck == -1">等待老师批阅</div>
    <div class="c-fs28 c-fc-xblack c-ww440 c-hh72 c-lh72 c-br8 c-bd1-gray c-textAlign-c c-center c-mt28" v-if="(examStatus==1 || examStatus==2) && (testRes.enableResurvey==1) && (testRes.isCheck != -1)" @click="testAgain">重新测试</div>
    <div class="c-fs24 theme-fc c-textAlign-c c-mt190 c-w100 c-pa c-p-b60 c-p-l0" v-if="testRes.nextId==0" @click="clickGoList">返回测试列表</div>
    <div class="c-fs24 c-fc-gray c-textAlign-c c-mt190 c-w100 c-pa c-p-b60 c-p-l0" v-if="examStatus==1 &&testRes.nextId!=0" @click="clickNextTest">下一测试</div>
  </div>
</template>

<script>
import { utilJs } from "@/utils/common.js"

export default {
  name: "NextExamination",
  components: {
  },
  props: [],
  data() {
    return {
      theme: localStorage.getItem("colorName") ? localStorage.getItem("colorName") : 'mb5_default',
      paperMainId: -1,
      topicId: -1,
      goOnNum: -1,
      type: -1,
      examStatus: -1,
      testRes: null,
      isBarrier: -1, //1: 闯关模式
      isFitCondition: false
    };
  },
  created() {
    utilJs.appShare(this);
  },
  activated() {
    this.testRes = null;
    this.paperMainId = this.$route.query.nextPaperMainId;
    this.topicId = this.$route.query.topicId;
    this.goOnNum = this.$route.query.goOnNum;
    this.type = this.$route.query.type;
    this.examStatus = this.$route.query.examStatus;
    this.isFitCondition = false;
    this.getExamInfo();

    utilJs.getMethod(global.apiurl + 'exam/topicDetail/' + this.topicId, (res) => {
      if (res.permission != 1) {
        this.$routerGo(this, "push", {
          path: "/homePage/examination/examinationDetail",
          query: { ExaminationNo: this.topicId }
        });
      }
      this.examinationInfo = res.topicInfo;
      this.wechatShare();
    })
  },
  deactivated() {
  },
  mounted() {

  },
  watch: {
    $route(to, from) {
      if (to.path == '/homePage/examination/nextExamination') {
        this.paperMainId = this.$route.query.nextPaperMainId;
        this.topicId = this.$route.query.topicId;
        this.goOnNum = this.$route.query.goOnNum;
        this.type = this.$route.query.type;
        this.examStatus = this.$route.query.examStatus;
        this.isFitCondition = false;
        this.getExamInfo();
      }
    }
  },
  methods: {
    getExamInfo() {
      let $this = this;
      let eUrl = this.examStatus == 0 || this.examStatus == 2 ? global.apiurl + 'exam/paperInfo/' + this.paperMainId + '?topicId=' + this.topicId
        : global.apiurl + 'exam/completeTest/' + this.paperMainId + '?topicId=' + this.topicId;
      utilJs.getMethod(eUrl, function (res) {
        $this.testRes = res;
        $this.isBarrier = res.isBarrier;
        setDocumentTitle($this.testRes.name);// eslint-disable-line
        if ($this.examStatus == 0 || $this.examStatus == 2) {
          $this.testRes.examStatus = res.nextExamStatus;
          $this.testRes.goOnNum = res.nextGoOnNum;
          return
        }
        $this.examMainId=res.examMainId;// eslint-disable-line
      });
    },
    testNow() {
      let $this = this;
      if (this.testRes.allNum <= 0) {
        this.$showCjToast({
          text: "该考试暂无题目，请联系管理员",
          type: 'warn',
        });
      } else {
        utilJs.getMethod(global.apiurl + 'paper/judgeEntry/' + $this.paperMainId + '?topicId=' + $this.topicId, function (res) {
          if (res.status == 1) {
            utilJs.postMethod(global.apiurl + "paperLibrary/createExam", {
              paperMainId: $this.paperMainId,
              topicId:$this.topicId,
              entryProdType:2,
              prodId:$this.topicId
            }, res => {
              $this.$routerGo($this, "replace", {
                path: "/homePage/examination/examinationAnswer",
                query: { paperMainId: $this.paperMainId, topicId: $this.topicId, goOnNum: 1 }
              });
            })
          } else {
            $this.$showCjToast({
              text: res.msg + '</br>' + res.showTime,
              type: 'warn'
            });
          }
        })
      }
    },
    goOnTest() {
      this.$routerGo(this, "replace", {
        path: "/homePage/examination/examinationAnswer",
        query: { paperMainId: this.paperMainId, topicId: this.topicId, goOnNum: this.goOnNum }
      });
      //    }
    },
    analyze() {
      this.$routerGo(this, "push", {
        path: '/homePage/examination/examinationAnswerAnalyze',
        query: {examMainId:this.examMainId, paperMainId: this.paperMainId, topicId: this.topicId, num:1}
      });
    },
    testAgain() {
      let nt = (new Date()).getTime();
      let lt = utilJs.getTimeByDate(this.testRes.lastJoinTime);
      if ((this.testRes.intervalTime != 0) && (lt + this.testRes.intervalTime * 60 * 60 * 1000 > nt)) {
        let interval = lt + (this.testRes.intervalTime * 60 * 60 * 1000) - nt;
        let secondTime = parseInt(interval / 1000);
        let minuteTime = 0;
        let hourTime = 0;
        if (secondTime > 60) {
          minuteTime = parseInt(secondTime / 60);
          secondTime = parseInt(secondTime % 60);
          if (minuteTime > 60) {
            hourTime = parseInt(minuteTime / 60);
            minuteTime = parseInt(minuteTime % 60);
          }
        }
        let showTime = minuteTime > 0 ? (hourTime > 0 ? (hourTime + '时' + minuteTime + '分' + secondTime + '秒') : (minuteTime + '分' + secondTime + '秒')) : (secondTime + '秒')
        this.$showCjToast({
          text: '请在' + showTime + '后重测',
          type: "text",
        });
      } else {
        let $this = this;
        utilJs.postMethod(global.apiurl + "paperLibrary/createExam", {
          paperMainId: $this.paperMainId,
          topicId:$this.topicId,
        }, res => {
          $this.$routerGo($this, "replace", {
            path: "/homePage/examination/examinationAnswer",
            query: { paperMainId: $this.paperMainId, topicId: $this.topicId, goOnNum: 1 }
          });
        })
      }
    },
    clickNextTest() {
      console.log((this.isFitCondition && this.examStatus != 0) || (this.isBarrier != 1))
      if (this.isBarrier == 1) {
        if (this.testRes.scoringMethod == 1) {
          if (Number(this.testRes.accuracy) * 100 >= Number(this.testRes.condition)) {
            this.isFitCondition = true;
          } else {
            this.isFitCondition = false;
          }
        } else if (this.testRes.scoringMethod == 2) {
          if (Number(this.testRes.totalpoins) >= Number(this.testRes.condition)) {
            this.isFitCondition = true;
          } else {
            this.isFitCondition = false;
          }
        } else if (this.testRes.scoringMethod == 3) {
          if (Number(this.testRes.rightNum) >= Number(this.testRes.condition)) {
            this.isFitCondition = true;
          } else {
            this.isFitCondition = false;
          }
        }
      }
      if ((this.isFitCondition && this.examStatus != 0) || (this.isBarrier != 1)) {
        this.$routerGo(this, "replace", {
          path: '/homePage/examination/nextExamination',
          query: { nextPaperMainId: this.testRes.nextId, topicId: this.topicId, goOnNum: this.testRes.goOnNum, type: this.testRes.type, examStatus: this.testRes.examStatus }
        });
      } else {
        this.$showCjToast({
          text: "下一测试暂未解锁",
          type: 'warn',
        });
      }
    },
    clickGoList() {
      this.$router.go(-1);
    },
    //分享
    wechatShare: function () {
      let title = this.examinationInfo.name;
      let shareImg = this.examinationInfo.avatar || require('../../../assets/examination_defult.png');
      let shareUrl = `${window.location.href.split("#")[0]}#/homePage/examination/examinationDetail?ExaminationNo=${this.topicId}&refereeId=${localStorage.getItem("userId")}`;
      let shareDesc = this.examinationInfo.shareDesc;//"共包含"+this.examinationInfo.hasPaper+"套考卷";
      utilJs.wechatConfig(shareUrl, title, shareImg, shareDesc, function () { });
    },
    //手机端分享
    appShare: function () {
      let title = this.examinationInfo.name;
      let shareImg = this.examinationInfo.avatar || require('../../../assets/examination_defult.png');
      let shareUrl = `${window.location.href.split("#")[0]}#/homePage/examination/examinationDetail?ExaminationNo=${this.topicId}&refereeId=${localStorage.getItem("userId")}`;
      let shareDesc = this.examinationInfo.shareDesc;//"共包含"+this.examinationInfo.hasPaper+"套考卷";
      utilJs.appShareTrue(title, shareImg, shareUrl, shareDesc);
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped>
</style>